from datetime import datetime
from sqlalchemy import String, DateTime, Integer, Text, DECIMAL, SmallInteger, JSON, CheckConstraint
from sqlalchemy.orm import Mapped, mapped_column
from config.database import Base


class SpeechRecordDO(Base):
    """
    语音识别记录表 
    """
    __tablename__ = 'speech_record'

    speech_id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True, comment='语音记录ID')
    user_id: Mapped[int] = mapped_column(Integer, nullable=True, comment='系统用户ID（内部用户）')
    client_user_id: Mapped[int] = mapped_column(Integer, nullable=True, comment='客户端用户ID（外部用户）')
    user_type: Mapped[int] = mapped_column(SmallInteger, nullable=False, default=1, comment='用户类型：1-系统用户, 2-客户端用户')
    session_id: Mapped[str] = mapped_column(String(64), nullable=True, comment='会话ID（用于实时识别）')
    file_name: Mapped[str] = mapped_column(String(255), nullable=True, comment='语音文件名')
    file_path: Mapped[str] = mapped_column(String(500), nullable=True, comment='语音文件路径')
    file_size: Mapped[int] = mapped_column(Integer, nullable=True, comment='文件大小（字节）')
    duration: Mapped[float] = mapped_column(DECIMAL(10, 2), nullable=True, comment='音频时长（秒）')
    model_name: Mapped[str] = mapped_column(String(100), nullable=False, comment='使用的识别模型')
    language: Mapped[str] = mapped_column(String(10), nullable=True, default='zh', comment='语言类型')
    recognize_text: Mapped[str] = mapped_column(Text, nullable=True, comment='识别结果文本')
    confidence: Mapped[float] = mapped_column(DECIMAL(5, 4), nullable=True, comment='识别置信度')
    status: Mapped[int] = mapped_column(SmallInteger, nullable=False, default=0, comment='状态：0-处理中, 1-成功, 2-失败')
    error_message: Mapped[str] = mapped_column(Text, nullable=True, comment='错误信息')
    recognition_config: Mapped[dict] = mapped_column(JSON, nullable=True, comment='识别配置参数')
    recognition_details: Mapped[dict] = mapped_column(JSON, nullable=True, comment='详细识别结果')
    api_request_id: Mapped[str] = mapped_column(String(64), nullable=True, comment='API请求ID（用于外部客户端）')
    callback_url: Mapped[str] = mapped_column(String(500), nullable=True, comment='回调地址（用于异步通知）')
    callback_status: Mapped[int] = mapped_column(SmallInteger, nullable=False, default=0, comment='回调状态：0-未回调, 1-回调成功, 2-回调失败')
    create_by: Mapped[str] = mapped_column(String(64), nullable=False, default='', comment='创建者')
    create_time: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_by: Mapped[str] = mapped_column(String(64), nullable=False, default='', comment='更新者')
    update_time: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    del_flag: Mapped[int] = mapped_column(SmallInteger, nullable=False, default=0, comment='删除标志：0-未删除, 1-已删除')
    remark: Mapped[str] = mapped_column(String(500), nullable=True, comment='备注')

    # 添加约束：确保 user_id 和 client_user_id 二选一
    __table_args__ = (
        CheckConstraint(
            '(user_id IS NOT NULL AND client_user_id IS NULL) OR (user_id IS NULL AND client_user_id IS NOT NULL)',
            name='chk_speech_record_user'
        ),
    ) 